package missObjects;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

import db.RawData;

/**
 * To handle generic Rof Mission Objects. Mother of all object classes
 * 
 * @author lefuneste
 *
 */
public class RofMissionObject {
	protected String objectType;
	protected int groupID;
	protected int indexInfile;
	protected int fileID;
	protected String name;
	protected String mapID;
	protected double Xpos;
	protected double Ypos;
	protected double Zpos;
	protected double Xori;
	protected double Yori;
	protected double Zori;
	protected String model;
	protected List<RawData> unProcessedData;

	public double getXpos() {
		return Xpos;
	}

	public void setXpos(double xpos) {
		Xpos = xpos;
	}

	public double getZpos() {
		return Zpos;
	}

	public void setZpos(double zpos) {
		Zpos = zpos;
	}
	
	
	public RofMissionObject() {
		unProcessedData = new ArrayList<RawData>();
	}

	public String getObjectType() {
		return objectType;
	}
	
	public int getIndexInfile() {
		return indexInfile;
	}

	
	
	public double getYori() {
		return Yori;
	}

	public void setYori(double yori) {
		Yori = yori;
	}

	/**
	 * Write an Object to the mission file
	 * @param path : path to the mission file
	 * @param currentIndex : index in mission file
	 * @return next index in mission file
	 * @throws IOException 
	 */
	public int writeToFile(Path path, int index, int linkTrID) throws IOException {
		try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.ISO_8859_1,StandardOpenOption.APPEND)) {
			this.writeStdPartToFile(writer, index,linkTrID);
			this.writeRawToFile(writer, index);
			//index++;
			return (index+1);
		}
		
	}
	
	/**
	 * write standard part of an Object into mission file
	 * @param writer : BufferedWriter to mission file
	 * @param index : number of object in mission file
	 * @throws IOException
	 */
	public void writeStdPartToFile (BufferedWriter writer, int index, int linkTrID) throws IOException {
		//standard object header
		String line=this.objectType+"\r\n"+
				"{\r\n"+
				"  Name = "+this.name+";\r\n"+
				"  Index = "+index+";\r\n"+
				"  LinkTrId = "+linkTrID+";\r\n"+
				"  XPos = "+Double.toString(Math.round(this.Xpos*1000)/1000.0)+";\r\n"+
				"  YPos = "+Double.toString(Math.round(this.Ypos*1000)/1000.0)+";\r\n"+
				"  ZPos = "+Double.toString(Math.round(this.Zpos*1000)/1000.0)+";\r\n"+
				"  XOri = "+Double.toString(Math.round(this.Xori*1000)/1000.0)+";\r\n"+
				"  YOri = "+Double.toString(Math.round(this.Yori*1000)/1000.0)+";\r\n"+
				"  ZOri = "+Double.toString(Math.round(this.Zori*1000)/1000.0)+";\r\n"+
				"";
		writer.write(line);
	}
	
	public void writeRawToFile (BufferedWriter writer, int index) throws IOException {
		//raw data
		for (RawData data : unProcessedData) {
			writer.write("  "+data.getLine()+"\r\n");
		}
		writer.write("}\r\n\r\n\r\n");
	}
}
